package com.ndp.fb.mq.producer;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.ndp.fb.config.ConfigCentre;
import com.ndp.fb.constant.CommonConstants;
import com.ndp.fb.enums.NotificationMessageKey;
import com.ndp.fb.enums.message.ChannelType;
import com.ndp.fb.enums.message.EventNotificationType;
import com.ndp.fb.enums.message.MessageStatus;
import com.ndp.fb.exception.ResourceNotificationMsgManager;
import com.ndp.fb.model.fbagent.param.ApiCallBackTarget;
import com.ndp.fb.mq.api.producer.EventNotificationMessageProducerService;
import com.ndp.fb.mq.api.producer.RocketMqProducerService;
import com.ndp.fb.rdb.model.message.EventNotificationMessage;
import com.ndp.fb.util.ListUtil;
import com.ndp.fb.util.UUID;
import com.restfb.util.StringUtils;

/**
 * Created by elvisxiong on 7/24/15.
 */
@Component
public class EventNotificationMessageProducerServiceImpl implements EventNotificationMessageProducerService {

    @Autowired
    private RocketMqProducerService rocketMqProducerService;

    /**
     * eventValues 默认使用汉语
     * @param receiver
     * @param eventNotificationKey
     * @param eventValues
     * @param url
     */
    public void sendMessage(String receiver, NotificationMessageKey eventNotificationKey, List<String> eventValues,String url) {
        ApiCallBackTarget.Target target = ApiCallBackTarget.MessageCenter.EventNotificationMessage;
        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, String> messageMap = new HashMap<String, String>();

        EventNotificationType eventNotificationType = null;
        switch (eventNotificationKey) {
            case N_001:
            case N_009:
                eventNotificationType = EventNotificationType.ANNOUNCEMENT;
                break;
            case N_008:
                eventNotificationType = EventNotificationType.AD_PUBLISH_FAILURE;
                break;
            case N_002:
            case N_004:
            case N_007:
            case N_005:
            case N_010:
            case N_011:
            case N_012:
                eventNotificationType = EventNotificationType.AUTHORITY_ALTERATION;
                break;
            case N_003:
            case N_006:
                eventNotificationType = EventNotificationType.TOKEN_INVALID;
                break;
        }

        messageMap.put("eventType", eventNotificationType.toString());
        messageMap.put("sender", ConfigCentre.getString(CommonConstants.MESSAGE_SENDER));
        messageMap.put("eventKey", eventNotificationKey.getCode());


        if(ListUtil.isEmpty(eventValues)){
            messageMap.put("eventValues", null);
        } else {
            Map<String,List<String>> eventValuesMap = new HashMap<>();
            eventValuesMap.put(ResourceNotificationMsgManager.CN, eventValues);
            eventValuesMap.put(ResourceNotificationMsgManager.US, eventValues);
            messageMap.put("eventValues", JSON.toJSONString(eventValuesMap));
        }

        messageMap.put("receiver", receiver);
        messageMap.put("channelTypes", ChannelType.WEBSITE.ordinal()+"");
        messageMap.put("url", url);
        if (StringUtils.isBlank(url)) {
            messageMap.put("is_need_function_jump", "true");
        } else {
            messageMap.put("is_need_function_jump", "false");
        }

//        map.put("body",body);
        map.put("equipment", messageMap);
        rocketMqProducerService.sendMessage(target.getModuleName(), target.getTargetType(), UUID.get(), map);
    }

    public void sendMonitorMessage(String receiver, String body, List<ChannelType> channelTypeList){
        if(ListUtil.isEmpty(channelTypeList))
            return;
    	ApiCallBackTarget.Target target = ApiCallBackTarget.MessageCenter.EventNotificationMonitorMessage;
        EventNotificationMessage eventNotificationMessage = new EventNotificationMessage();
        EventNotificationType eventNotificationType = EventNotificationType.ANNOUNCEMENT;
        eventNotificationMessage.setEventNo(com.ndp.fb.util.UUID.get());
        eventNotificationMessage.setEventType(eventNotificationType.getType());
        eventNotificationMessage.setIsNeedFunctionJump(false);
        eventNotificationMessage.setSender("MediaBuy系统");
        eventNotificationMessage.setReceiver(receiver);
        eventNotificationMessage.setIsRead(0);
        eventNotificationMessage.setBody(body);
        eventNotificationMessage.setSendTime(System.currentTimeMillis());
        eventNotificationMessage.setStatus(MessageStatus.NORMAL.getStatus());
        Map<String, Object> map = new HashMap<>();
        map.put("body", eventNotificationMessage);
        Map<String, String> channelMap = new HashMap<>();
        StringBuffer sb = new StringBuffer();
        for(int i=0;i<channelTypeList.size();i++) {
            sb.append(channelTypeList.get(i));
            if(i < channelTypeList.size() -1)
                sb.append(",");
        }
        channelMap.put("chanel", sb.toString());
        map.put("equipment", channelMap);
        rocketMqProducerService.sendMessage(target.getModuleName(), target.getTargetType(), UUID.get(), map);
        
    }
}
